package cn.edu.zafu.bigdata.lb.student;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class StudentManagementSystem {
    private static final String FILE_NAME = "src/main/java/cn/edu/zafu/bigdata/lb/student/student.txt";
    private final List<Student> students;
    private final Scanner scanner;


    public StudentManagementSystem() {
        students = new ArrayList<>();
        scanner = new Scanner(System.in);
        loadStudents();
    }

    public void start() {
        int choice;
        do {
            printMenu();
            choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline
            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    searchStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    System.out.println("退出程序。");
                    break;
                default:
                    System.out.println("无效的操作编号，请重新输入。");
            }
        } while (choice != 5);
        saveStudents();
    }

    private void printMenu() {
        System.out.println("======= 学生信息管理系统 =======");
        System.out.println("1. 添加学生信息");
        System.out.println("2. 检索学生信息");
        System.out.println("3. 更新学生信息");
        System.out.println("4. 删除学生信息");
        System.out.println("5. 退出程序");
        System.out.print("请输入操作编号 (1-5): ");
    }

    private void addStudent() {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (findStudentById(id) != null) {
            System.out.println("学号已存在，请重新输入。");
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        students.add(new Student(id, name, phone));
        System.out.println("学生信息添加成功。");
    }

    private void searchStudent() {
        while (true) {
            System.out.print("请输入学号进行检索（输入'QUIT'退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = findStudentById(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void updateStudent() {
        while (true) {
            System.out.print("请输入要更新的学生学号（输入'QUIT'退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = findStudentById(id);
            if (student != null) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                System.out.println("学生信息更新成功。");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void deleteStudent() {
        while (true) {
            System.out.print("请输入要删除的学生学号（输入'QUIT'退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = findStudentById(id);
            if (student != null) {
                students.remove(student);
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private Student findStudentById(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }

    private void loadStudents() {
        File file = new File(FILE_NAME);
        if (file.exists()) {
            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
                Object object = ois.readObject();
                List<Student> students = new ArrayList<>();
if (object instanceof List<?> list) {
    for (Object o : list) {
        if (o instanceof Student student) {
            students.add(student);
        } else {
            System.err.println("数据异常");
        }
    }
} else {
    System.err.println("数据异常");
}

            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
    }


    private void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
    }

    public static void main(String[] args) {
        StudentManagementSystem system = new StudentManagementSystem();
        system.start();
    }
}